<?php
namespace Content\Controller;

use Base\Controller\HomeBaseController; 

class IndexController extends HomeBaseController {

	protected $categoryModel;
	
	function _initialize() {
		parent::_initialize();
		
		$this->categoryModel = D("Content/Category");
	}
	
    // 首页
	public function index() {
    	$this->display(":index");
		return;
    }

	// 前台POST列表及单页
	public function lists() {
	    $categoryId = I('get.cid', 0, 'intval');

	    // get category
	    $category = $this->categoryModel->where(array("id" => $categoryId))->find();
		if (empty($category)) {
			header('HTTP/1.1 404 Not Found');
			header('Status:404 Not Found');
			
			$msg = 'CATEGORY ID: ' . $categoryId . ' NOT FOUND';
			$this->assign("msg", $msg);
			$this->display("../Public:404");
			return;
		}

		$this->assign("category", $category);                             // CAT
		$this->assign("CID", $category['id']);                            // CAT ID
		
		// get contentModel
		$modelTableName = $category['model_table_name'];
		$contentModel = M($modelTableName);
		
		// get template name and data
		if ($category['type'] == 1) {										// 栏目
			$template = $category["tpl_column"];
			
			// cid and where
			$where = "cid = $categoryId and status = 1";
			
			$this->assign("where", $where);
		} else if ($category['type'] == 2) {								// 列表
			$template = $category["tpl_list"];
			
			// cid and where
			$where = "cid = $categoryId and status = 1";
			
			$this->assign("where", $where);
		} else if ($category['type'] == 3) {                                // 单页
			$template = $category["tpl_single"];

			$where['cid'] = $categoryId;
			$where['status'] = 1;
			$content = $contentModel->where($where)->find();
			if (empty($content)) {
				header('HTTP/1.1 404 Not Found');
				header('Status:404 Not Found');
				
				$msg = 'SINGLE PAGE ID: ' . $categoryId . ' NOT FOUND';
				$this->assign("msg", $msg);
				$this->display("../Public:404");
				return;
			}

			$this->assign($content);
		}
		
		$this->display(":$template");
		return;
	}

    // 前台content单页（内容页）
	public function show() {
		$categoryId = I('get.cid', 0, 'intval');
		$id = I('get.id', 0, 'intval');
		
	    // get category
	    $category = $this->categoryModel->where(array("id" => $categoryId))->find();		// 暂时只考虑不包含子项的情况
		if (empty($category)) {
			header('HTTP/1.1 404 Not Found');
			header('Status:404 Not Found');
			
			$msg = 'CATEGORY ID: ' . $categoryId . ' NOT FOUND';
			$this->assign("msg", $msg);
			$this->display("../Public:404");
			return;
		}

		$this->assign("CID", $category['id']);                           // CAT ID
		
		// get contentModel
		$modelTableName = $category['model_table_name'];
		$contentModel = M($modelTableName);
		
		// get template name
		$template = $category["tpl_single"];

		////// GET OBJ
		// $where
		$where['id'] = $id;
		$where['status'] = 1;
		
		// view updated
		$contentModel->where($where)->setInc('view_count', 1);

		// get obj
		$content = $contentModel->where($where)->find();
		if (empty($content)) {
			header('HTTP/1.1 404 Not Found');
			header('Status:404 Not Found');
			
			$msg = 'CONTENT ID: ' . $id . ' NOT FOUND';
			$this->assign("msg", $msg);
			$this->display("../Public:404");
			return;
		}

		$this->assign($content);

		////// GET PREV & NEXT
		$prev = $contentModel->where("id < $id and cid = $categoryId and status = 1")->order('id desc')->limit('1')->find();
		$this->assign('prev', $prev);
		$next = $contentModel->where("id > $id and cid = $categoryId and status = 1")->order('id asc')->limit('1')->find();
		$this->assign('next', $next);

		$this->display(":$template");
		return;
	}

}
